﻿package com.saint.video.videoControls {
	
	import com.saint.utils.events.ButtonEvent;
	import com.saint.utils.ui.GraphicBttn;
	import com.saint.utils.Logger;
	import com.saint.video.events.VideoControlsEvent;
	import com.saint.video.events.VideoSliderEvent;
	import com.saint.video.SimpleVideo;
	import flash.display.Sprite;
	import com.saint.utils.events.UpdateEvent;
	import com.saint.utils.events.ActiveMouseAreaEvent;
	import com.saint.video.events.VideoEvent;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import com.saint.utils.TimeUtils;
	
	public class VideoControls extends Sprite{
		
		private var playButton:GraphicBttn;
		private var pauseButton:GraphicBttn;
		private var _targetVideo:SimpleVideo;
		private var scrubber:VideoSlider;
		private var timeDisplay:VideoTimeDisplay;
		private var volumeControl:VolumeControl;
		
		private var watch:Timer;
		private var assetsWidth:Number;
		private var controlsY:uint;
		
		public function VideoControls(videoDisplay:SimpleVideo, _width:Number, _height:Number, autoPlay:Boolean = true):void{
			Logger.info("VideoControls loaded ");
			
			mouseChildren = true;
			
			drawBg(_width, _height);
			assetsWidth = 145;
			_targetVideo = videoDisplay;
			
			playButton = new GraphicBttn(play_off);
			pauseButton = new GraphicBttn(pause_off);
			
			controlsY = (_height - playButton.height) / 2;
			
			playButton.x = pauseButton.x = 10;
			playButton.y = pauseButton.y = controlsY;
			
			scrubber = new VideoSlider( _width - assetsWidth);
			scrubber.addEventListener(VideoSliderEvent.SCRUBSTART, onScrubberPress);
			scrubber.addEventListener(VideoSliderEvent.SCRUBSTOP, onScrubberStop);
			scrubber.addEventListener(VideoSliderEvent.SCRUBBING, onScrub);
			scrubber.x = playButton.x + playButton.width + 15;
			scrubber.y = playButton.y;
			
			
			timeDisplay = new VideoTimeDisplay();
			timeDisplay.x = scrubber.x + scrubber.width;
			timeDisplay.y = scrubber.y - 1;
			
			volumeControl = new VolumeControl();
			volumeControl.x = timeDisplay.x + timeDisplay.width - 32;
			volumeControl.y = playButton.y + 1;
			volumeControl.addEventListener(ActiveMouseAreaEvent.MOUSE_MOVE, volumeHandler, false, 0, true);
			
			playButton.addEventListener(ButtonEvent.BUTTON_RELEASE, playHandler, false, 0, true);
			pauseButton.addEventListener(ButtonEvent.BUTTON_RELEASE, pauseHandler, false, 0, true);
			
			addChild(timeDisplay);
			addChild(scrubber);
			
			(autoPlay) ? addChild(pauseButton) : addChild(playButton);
			
			addChild(volumeControl);
			
		//	startWatching();
		}
		
		

		
/**
 * HANDLERS
 */
		public function videoStreamUpdateHandler(perc:Number):void {
			scrubber.updateStreaming(perc);
		}
		
		public function endOfStreamHandler(event:VideoEvent):void {
			pauseHandler(null);
		}
		
		public function videoEventHandler(time:Number):void {
			Logger.info("VideoControls.videoEventHandler " + time);
			timeDisplay.duration = TimeUtils.formatedTimerCount(time);
			scrubber.duration = time;
		}
		
		public function timerHandler(currentTime):void {
			timeDisplay.updateTime(TimeUtils.formatedTimerCount(currentTime));
			scrubber.updatePlayHead(currentTime);
		}
		
		
		/**
		 * PLAYBUTTON
		 * @param	event
		 */
		private function playHandler(event:ButtonEvent):void {
			Logger.info("VideoControls.playHandler");
					
			addChild(pauseButton);
			pauseButton.setEnabled(true);
			
			try {
				playButton.setEnabled(false);
				removeChild(playButton);
			} catch (event:Error) {
				Logger.info("VideoControls.playHandler() cannot remove a no-existant button");
			}
			
			//_targetVideo.resumeVideo();
			dispatchEvent(new VideoControlsEvent(VideoControlsEvent.PLAY));
		}
		
		
		/**
		 * PAUSE BUTTON
		 * @param	event
		 */
		private function pauseHandler(event:ButtonEvent):void {
			Logger.info("VideoControls.pauseHandler");
			
			
			addChild(playButton);
			playButton.setEnabled(true);
			
			try {
				pauseButton.setEnabled(false);
				removeChild(pauseButton);
			} catch (event:Error) {
				Logger.info("VideoControls.pauseHandler() cannot remove a no-existant button");
			}
			
			dispatchEvent(new VideoControlsEvent(VideoControlsEvent.PAUSE));
		}
		
		private function volumeHandler(event:ActiveMouseAreaEvent):void {
			dispatchEvent(new VideoControlsEvent(VideoControlsEvent.VOLUME_CHANGE, event.data));
		}
		
		
		
		
		
		/**
		 * SCRUBBER
		 * @param	e
		 */
		private function onScrubberPress(e:VideoSliderEvent):void
		{
			dispatchEvent(new VideoControlsEvent(VideoControlsEvent.SCRUBSTART));
		}
		
		private function onScrubberStop(e:VideoSliderEvent):void
		{
			dispatchEvent(new VideoControlsEvent(VideoControlsEvent.SCRUBSTOP, e.pos));
		}
		
		private function onScrub(e:VideoSliderEvent):void
		{
			dispatchEvent(new VideoControlsEvent(VideoControlsEvent.SCRUBBING, e.pos));
		}
		
	
		
/**
 * UTILITIES
 */
		private function drawBg(_width:Number, _height:Number):void {
			graphics.beginFill(0xFFFFFF);
			graphics.drawRect(0, 0, _width, _height);
			graphics.endFill();
			
		}
		
/**
 * GETTERS AND SETTERS
 */
		public function get targetVideo():SimpleVideo { return _targetVideo; }
		public function set targetVideo(value:SimpleVideo):void {
			_targetVideo = value;
		}
		
	}
}